import { CfgTable } from "./CfgTable";

export class ConfigDB {

    private static _version: number;
    private static _tableDict: { [key: string]: CfgTable } = {};
    private static _objDict: { [key: string]: any } = {};

    public constructor() {
    }

    public static load(data: { [key: string]: any }, objDataNames?: string[]) {
        this._version = data["version"];
        delete data["version"];
        if (objDataNames) {
            for (let tn of objDataNames) {
                this._objDict[tn] = data[tn];
                delete data[tn];
            }
        }
        for (let name in data) {
            let table = new CfgTable(name);
            this._tableDict[name] = table;
            table.load(data[name], "id");
        }
    }

    public static addTable(table: CfgTable) {
        this._tableDict[table.name] = table;
    }

    public static getTable(name: string): CfgTable {
        return this._tableDict[name];
    }

    public static getObj(name: string): any {
        return this._objDict[name];
    }

    public static get version() {
        return this._version;
    }

}